package com.leo.chapter15;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 用显式锁实现随机数生成器
 * @author xuexiaolei
 * @version 2017年10月20日
 */
public class AtomicPseudoRandom {
    private AtomicInteger seed;

    public AtomicPseudoRandom(int seed){
        this.seed = new AtomicInteger(seed);
    }

    public int nextInt(int n){
        while (true){
            int s = seed.get();
            int nextSeed = caclulateNext(s);
            if (seed.compareAndSet(s, nextSeed)){
                int remainder = s % n;
                return remainder > 0? remainder : remainder+n;
            }
        }
    }

    private int caclulateNext(int s) {
        return 0;
    }
}
